/*
*	Copyright (c) 2009 Brent Mombourquette
*
*	Permission is hereby granted, free of charge, to any person
*	obtaining a copy of this software and associated documentation
*	files (the "Software"), to deal in the Software without
*	restriction, including without limitation the rights to use,
*	copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the
*	Software is furnished to do so, subject to the following
*	conditions:
*
*	The above copyright notice and this permission notice shall be
*	included in all copies or substantial portions of the Software.
*
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*	OTHER DEALINGS IN THE SOFTWARE.
*
*	This file contains the functions to manipulate the Session Trail panel in graph_page.html.
*/

var sessionTrail = {
	/*
	 * Highlight the Session Trail entry and SVG edge that correspond to the database
	 * index idx.
	 * @param {Number} idx
	 */
	highlight: function(idx){
		// Entry tags correspond to rows in the database, so each entry's id is its row id
		var entryTag = document.getElementById(idx);
		entryTag.style.borderLeft = "10px solid #1c64d1";
		entryTag.style.borderRight = "10px solid #5ba3e0";
		entryTag.style.backgroundColor = "#2586d7";
		// Each edge corresponds to a row in the database, so each edge has an id in the
		// form edge:{database row number}
		var edgeElement = document.getElementById("edge:"+idx);
		if (edgeElement)
			edgeElement.setAttribute('stroke', '#FF0000');
	},
	/*
	 * Unhighlight the Session Trail entry and SVG edge that correspond to the database
	 * index idx.
	 * @param {Number} idx
	 */
	unhighlight: function(idx){
		// Entry tags correspond to rows in the database, so each entry's id is its row id
		var entryTag = document.getElementById(idx);
		entryTag.style.borderLeft = "10px solid #1958b7";
		entryTag.style.borderRight = "10px solid #508fc4";
		entryTag.style.backgroundColor = "#2175bc";
		// Each edge corresponds to a row in the database, so each edge has an id in the
		// form edge:{database row number}
		var edgeElement = document.getElementById("edge:"+idx);
		if (edgeElement)
			edgeElement.setAttribute('stroke', '#778899');
	},
	
	/*
	 * Create the DOM for this Session Trail entry (database entry). The entries ID is idx, and it displays site and time.
	 * @param {String} site
	 * @param {String} time
	 * @param {Number} idx
	 */
	createEntry: function(site, time, idx) {
		var list = document.getElementById("orderOfVisits");
		
		var li = document.createElement("li");
		// Each <li> has a tag which corresponds to li:{index of row in the database}
		li.id = "li:"+idx;
		
		var a = document.createElement("a");
		a.innerHTML = "To: " + site + "\nWhen: " + time;
		a.id = idx;
		a.onmouseover = new EventHandler( window, sessionTrail.highlight, idx);
		a.onmouseout = new EventHandler( window, sessionTrail.unhighlight, idx);
		a.onclick = new EventHandler( window, annotationEdit.preEdge, "edge:"+idx);
		// Add them to the DOM
		li.appendChild(a);
		list.appendChild(li);
	},

	/*
	 * Remove the entry in the Session Trail panel with id li:[id]. See SVGGraphView.prototype.removeNode function.
	 * @param {String} id 		The id of an edge being removed, so it is in the form "edge:#"
	 */
	removeEntry: function(id) {
		// Each <li> has an id of "li:#" so idsplit(":") results in idx[1] being the number from "edge:#"
		// which percisily corresponds to the <li> id
		var idx = id.split(":");
		var li = document.getElementById("li:"+idx[1]);
		if (li)
			li.parentNode.removeChild(li);
	},
	
	/*
	 * Remove all entries from the Session Trail panel.
	 */
	removeAllEntries: function() {
		var list = document.getElementById("orderOfVisits");
		if ( list.hasChildNodes() ) {
		    while ( list.childNodes.length >= 1 ) {
		        list.removeChild( list.firstChild );       
		    } 
		}
	}
}